#include "Log/test.h"
#include "Utility/CJson/cJSON.h"
#include "Utility/CJson/json_reflection_arg_macros.h"
#include "Utility/CJson/json_reflection_utility.h"
#include "cpp-httplib/httplib.h"
#include <cstddef>
#include <functional>
#include <string>
#include <vector>

#define LISTEN_PORT 8888

#include "Log/TraceLog.h"
#include "mDNS/mdns_query_parse.h"

#include "Utility/CJson/WebCommonOutputTemplate.h"

struct testApiOutputArray : JsonReflectionBase
{
    std::vector<std::string> arr;
    REFLECT_WEBJSON_OUTPUT_ARGS_NUM_1(arr)
};

struct testApiInput : JsonReflectionBase
{
    std::string input;
    REFLECT_WEBJSON_INPUT_ARGS_NUM_1(input)
};

struct testApiOutput : JsonReflectionBase
{
    std::string output;
    testApiOutputArray output_array;
    REFLECT_WEBJSON_OUTPUT_ARGS_NUM_2(output, output_array)
};

int testApiImpl(const testApiInput &input, testApiOutput &output, std::string &strDesc)
{
    TRACE_LOG_A(nullptr, TRACE_LOG_LEVEL_INFO, "input :%s", input.input.c_str());
    output.output = "fuck";
    output.output_array.arr.push_back("name1");
    output.output_array.arr.push_back("name2");
    return 0;
}

int testApiOper(cJSON *pJson, std::string &strJsonBuffer)
{
    testApiInput input;
    testApiOutput output;
    return GenaralOperator<testApiInput, testApiOutput>(pJson, strJsonBuffer, input, output,
                                                        std::bind(&testApiImpl, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
}

int main(int argc, char *argv[])
{
    TRACE_LOG_A(nullptr, TRACE_LOG_LEVEL_DEBUG, "Trace print");
    TRACE_LOG_A(nullptr, TRACE_LOG_LEVEL_INFO, "Trace print");
    TRACE_LOG_A(nullptr, TRACE_LOG_LEVEL_WARNING, "Trace print");
    TRACE_LOG_A(nullptr, TRACE_LOG_LEVEL_ERROR, "Trace print");
    testPrint();
    mdns_query_parse();

    httplib::Server svr;

    // 定义 POST 接口（处理 JSON）
    svr.Post("/login", [](const httplib::Request &req, httplib::Response &res) {
        if (req.has_param("username") && req.has_param("password"))
        {
            auto username = req.get_param_value("username");
            auto password = req.get_param_value("password");
            // 验证逻辑...
            res.set_content("Login successful: " + username, "text/plain");
        }
        else
        {
            res.status = 400;// Bad Request
        }
    });

    svr.Post("/test", [](const httplib::Request &req, httplib::Response &res) {
        std::string strResponse;
        res.status = 400;// Bad Request
        if (!req.body.empty())
        {
            cJSON *pJson = nullptr;
            pJson = cJSON_Parse(req.body.c_str());
            if (nullptr != pJson)
            {
                if (0 == testApiOper(pJson, strResponse))
                {
                    res.status = 200;
                    TRACE_LOG_A(nullptr, TRACE_LOG_LEVEL_INFO, "%s", strResponse.c_str());
                }
                cJSON_Delete(pJson);
            }
        }
        res.set_content(strResponse, "text/plain");
    });

    std::string str("Server started on port ");
    str += std::to_string(LISTEN_PORT);
    LogPrint(str);
    svr.listen("0.0.0.0", LISTEN_PORT);
    return 0;
}
